﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AlexandriaPressPublisherDataAccessLayer;
using System.Data;
using System.Web;
using System.IO;

namespace AlexandriaPressPublisherBusinessLayer
{
    public class ProofreaderProcessManager
    {
        public static string deadlineNotification(int id, ref int draftID, ref bool sendEmail)
        {
            string notificationString = "";
            DateTime dateAssigned = ProofreaderDataAccessManager.getDueDate(id, ref draftID);

            if (dateAssigned != DateTime.MinValue)
            {
                System.TimeSpan duration = new System.TimeSpan(14, 0, 0, 0);
                string dueDate = Convert.ToString(dateAssigned.Add(duration));
                if (dateAssigned.CompareTo(DateTime.Now) > 0)
                {
                    notificationString = "The deadline for the draft assigned to you was on " + dueDate + "; this draft is overdue!";
                    sendEmail = true;
                }
                else
                {
                    notificationString = "The deadline for the draft assigned to you is on " + dueDate;
                }
            }
            else
            {
                notificationString = "You do not have any pending draft to review.";
            }

            return notificationString;
        }

        public static string reviewedDraftUpload(string fileName, string path, HttpPostedFile fil, int userID, int draftID)
        {
            fileName = fileName.Trim();
            path = path.Trim();
            Boolean fileOK = false;
            String fileExtension = Path.GetExtension(fileName).ToLower();
            String[] allowedExtensions = { ".pdf" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }

            if (fileOK)
            {
                if (!(path.Length > 8000))
                {
                    FileInfo file = new FileInfo(path);

                    if (!file.Exists && !(fileName.Length > 100))
                    {
                        fil.SaveAs(path);
                        int x = ProofreaderDataAccessManager.saveReviewedDraft(path, userID, draftID);
                        if (x > 0)
                        {
                            return "File uploaded!";
                        }
                        else
                        {
                            return "File could not be uploaded.";
                        }
                    }
                    else
                    {
                        return "Please rename the file.";
                    }
                }
                else
                {
                    return "Error - Please call an administrator";
                }
            }
            else
            {
                return "Cannot accept files of this type.";
            }
        }

        public static DataTable getAllDrafts()
        {
            return ProofreaderDataAccessManager.selectAllUnassignedDrafts();
        }

        public static string downloadDraft(int slectedDraftID, int proofreaderID, ref int numHits, ref string destinationUrl)
        {
            string[] separators = { "\\" };
            string source = ProofreaderDataAccessManager.assignProofreader(slectedDraftID, proofreaderID, ref numHits);
            if (source != null)
            {
                string[] urlTokens = source.Split(separators, StringSplitOptions.RemoveEmptyEntries);
                if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Personal)))
                {
                    Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
                    destinationUrl = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Downloads\\" + urlTokens[urlTokens.Length - 1];
                }
                else
                {
                    destinationUrl = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + urlTokens[urlTokens.Length - 1];
                }
            }
            return source;
        }

        public static int checkForUnassignedUser(int userID)
        {
            return ProofreaderDataAccessManager.checkUnassignedUser(userID);
        }

        public static int submitProofreaderFindings(int userID, int draftID, string prooferRecommendation, string prooferComment)
        {
            return ProofreaderDataAccessManager.uploadDraft(userID, draftID, prooferRecommendation, prooferComment);
        }
    }
}
